use crate::tool::{Tool, ToolResult};
use std::collections::HashMap;

pub struct WeatherTool;

impl Tool for WeatherTool {
    fn name(&self) -> &str {
        "weather"
    }

    // 修正返回值为 Result<ToolResult, String>
    fn run(&self, params: &HashMap<String, serde_json::Value>) -> Result<ToolResult, String> {
        // 解析参数（使用 ? 运算符）
        let city = params.get("city")
            .and_then(|v| v.as_str())
            .ok_or("缺少参数city或格式错误")?;

        // 模拟天气查询
        let temperature = match city {
            "北京" => "25°C",
            "上海" => "28°C",
            "广州" => "30°C",
            _ => "未知",
        };

        // 返回成功结果
        Ok(ToolResult {
            success: true,
            content: format!("{}的天气：{}，晴朗", city, temperature),
        })
    }
}